<?php
namespace pfp\lib {
    
    \defined( '_JEXEC' ) or die( 'Restricted access' );

    /**
     * Description of progInit
     *
     * @author George Zeakis<zeageorge@google.com>
     */
    abstract class progInit {

        protected $_vars          = array();
        private   $_events        = array();
        private   $_alreadyLoaded = \FALSE;

        final public function __construct() {
            $this->_vars["className"]  = __CLASS__;
            $this->_vars["childClass"] = \get_called_class();
        }

        final public function setInitParams(array $params){
            $tmp = __FUNCTION__."_tmp"; if (isset($this->_vars["$tmp"])){ unset($this->_vars["$tmp"]); }
            try {
                if ($this->_alreadyLoaded){ return \TRUE; } else { $this->_alreadyLoaded = \TRUE; }
                $this->_registerEvents();
                $this->_vars["$tmp"]["initParams"] = $params;

                $eventData = array('event_name'=>$this->_events['event_onBefore_setInitParams'],'args'=>&$this->_vars); eventManager::riseEvent($eventData);

                foreach ($this->_vars["$tmp"]["initParams"] as $key => $value) { $this->{$key} = $value; }

                $eventData = array('event_name'=>$this->_events['event_onAfter_setInitParams'],'args'=>&$this->_vars); eventManager::riseEvent($eventData);
                unset($this->_vars["$tmp"]); 
                return \TRUE;
            } catch (codeExecution_FlowControlException $exc) {
                $this->_vars["$tmp"]["errors"] = $exc;
                $eventData = array('event_name'=>$this->_events['event_onError'],'args'=>&$this->_vars); eventManager::riseEvent($eventData);
                unset($this->_vars["$tmp"]); 
                throw new codeExecution_FlowControlException(array('return'=>\FALSE), $exc->getMessage()); return \FALSE;
            }
        }

        abstract public function Init();
        
        private function _registerEvents(){
            $this->_events = array(
                'event_onStart'   =>'event_onStart',
                'event_onError'   =>'event_onError',
                'event_onWarning' =>'event_onWarning',
                'event_onBefore_setInitParams'=>'event_onBefore_setInitParams',
                'event_onAfter_setInitParams'=>'event_onAfter_setInitParams',
            );
            eventManager::registerEvents($this->_events);
        }
        
        final public function __destruct() { }
    }
}
?>
